package cn.edu.neusoft.yuejian12.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import cn.edu.neusoft.yuejian12.Adapter.HactivityAdapter;
import cn.edu.neusoft.yuejian12.Adapter.HmasterAdapter;
import cn.edu.neusoft.yuejian12.JavaBean.Activity;
import cn.edu.neusoft.yuejian12.JavaBean.Master;
import cn.edu.neusoft.yuejian12.Model.ActivityModel;
import cn.edu.neusoft.yuejian12.Model.MasterModel;
import cn.edu.neusoft.yuejian12.R;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 木子李 on 2017/10/16.
 */

public class HmasterFragment extends Fragment {
    private RecyclerView recyclerView;
    private String userid;
    private Intent intent;
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        getUserId();
        recyclerView = (RecyclerView)view.findViewById(R.id.rv_hmaster);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        initData();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.h_master, container,false);


        return view;
    }

    private void getUserId(){
        intent = getActivity().getIntent();
        userid = intent.getStringExtra("uid");
    }

    private void initData(){
        getUserId();
        getHmaster();
    }
    private void getHmaster(){
        int uid = Integer.parseInt(userid);

        MasterModel masterModel = new MasterModel();
        Call<List<Master>> call = masterModel.getAllmaster();
        call.enqueue(new Callback<List<Master>>() {
            @Override
            public void onResponse(Call<List<Master>> call, Response<List<Master>> response) {
                List<Master> mDataList = response.body();
                recyclerView.setAdapter(new HmasterAdapter(HmasterFragment.this.getContext(),mDataList));//设置适配器，绑定Adapter.


            }

            @Override
            public void onFailure(Call<List<Master>> call, Throwable t) {
                Toast.makeText(getActivity(),"fail",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
